program BuildVersionFile;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, uDZSysUtils, uDZFile, uDZMD5, uCxtTypes, uDZPersistent;

function MD5File(const FileName: string): AnsiString;
var
  Digest: TMD5Digest;
begin
  uDZMD5.MD5File(FileName, Digest);
  Result := MemHex(Digest, SizeOf(Digest));
end;

var
  ExeFile, FileName: string;
  MetaData: TMetaData;
  FileVer: TDzFileVersionInfo;
begin
  FileName := ExtractFilePath(ParamStr(0)) + 'MetaData.xml';
  ExeFile := ExtractFilePath(ParamStr(0)) + 'cxt.exe';
  MetaData := TMetaData.Create;
  try
    FileVer := TDzFileVersionInfo.Create(ExeFile);
    try
      if FileExists(FileName) then
        MetaData.LoadFromFile(FileName, afXML);
      MetaData.Version.Assign(FileVer.Version);
      MetaData.PackageMD5 := MD5File(ExeFile);
      MetaData.SaveToFile(FileName, afXML);
    finally
      FileVer.Free;
    end;
  finally
    MetaData.Free;
  end;
end.
